/*
 * Copyright 2009-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.googlecode.viewt.core.contant;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 *
 * @author Steven Li
 */
public abstract class AbstractBaseConstant implements IConstant {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8095897754100277737L;
	public static final String APPEND_FLAG = ".";
	public static final String APPEND_DISPLAY = "display";
	public static final String APPEND_INPUT = "input";
	private String key;
	private String value;
	private String displayValue;
	private String inputCode;

	public AbstractBaseConstant() {
		this.key = null;

		this.value = null;

		this.displayValue = null;

		this.inputCode = null;
	}

	/* (non-Javadoc)
	 * @see org.googlecode.viewt.base.contant.IConstant#getKey()
	 */
	public String getKey() {
		return this.key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return this.value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getDisplayValue() {
		return this.displayValue;
	}

	public String getDisplayValue(Locale locale) {
		if (null == locale)
			return getDisplayValue();
		if (Locale.getDefault().toString().equals(locale.toString())) {
			return getDisplayValue();
		}

		ResourceBundle bundle = constBundle(defaultResourceUrl(), locale);
		if (null != bundle) {
			return bundle.getString(getKey() + "." + "display");
		}

		return null;
	}

	public void setDisplayValue(String displayValue) {
		this.displayValue = displayValue;
	}

	public String getInputCode() {
		return this.inputCode;
	}

	public void setInputCode(String inputCode) {
		this.inputCode = inputCode;
	}

	protected static ResourceBundle constBundle(String resourceUrl,
			Locale locale) {
		if ((null == resourceUrl) || ("".equals(resourceUrl)))
			return null;
		if (null == locale) {
			locale = Locale.getDefault();
		}
		try {
			return ResourceBundle.getBundle(resourceUrl, locale);
		} catch (MissingResourceException resEx) {
		}
		return null;
	}

	protected static String bundleString(ResourceBundle bundle, String key) {
		try {
			return bundle.getString(key);
		} catch (MissingResourceException resEx) {
		}
		return null;
	}

	protected static boolean isConstKey(String resourceKey) {
		if (null == resourceKey) {
			return false;
		}

		return (resourceKey.indexOf(".") >= 0);
	}

	public String toString() {
		return "key=" + getKey() + ", value=" + getValue() + ", display="
				+ getDisplayValue() + ", input=" + getInputCode();
	}

	protected abstract String defaultResourceUrl();
}